<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Wechat;
use app\common\manager\assistant\AssistantAccountManager;
use app\common\manager\assistant\AssistantManager;
use app\common\manager\BaseManager;
use think\Config;
use think\Exception;

/**
 * 队长中心
 */
class Assistant extends Api
{
    protected $noNeedLogin = ['getAssistantList'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 队长个人信息及中心信息
     */
    public function apply()
    {
        $address = $this->request->param('address');
        $latitude = $this->request->param('latitude');
        $longitude = $this->request->param('longitude');
        $gender = $this->request->param('gender');
        $avatar = $this->request->param('avatar');
        try {
            $result = AssistantManager::apply($this->user,$address,$latitude,$longitude,$gender,$avatar);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 获取入驻信息
     * @return void
     */
    public function applyInfo()
    {
        try {
            $result = AssistantManager::getApplyInfo($this->user);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 获取助教信息
     * @return void
     */
    public function assistantInfo(){
        try {
            $result = AssistantManager::getAssistantInfo($this->user);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 获取照片墙
     * @return void
     */
    public function assistantImage()
    {
        $page = $this->request->param('page',1);
        try {
            $result = AssistantManager::getAssistantImage($this->user,$page);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 详情获取照片墙
     * @return void
     */
    public function DetailImage()
    {
        $page = $this->request->param('page',1);
        $assistantId = $this->request->param('assistantId',0);
        try {
            $result = AssistantManager::AssistantImages($assistantId,$page);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 添加照片墙
     * @return void
     */
    public function addAssistantImage(){
        $url = $this->request->param('url');
        try {
            $result = AssistantManager::addAssistantImage($this->user,$url);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'],$result['data']);
            } else {
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 删除照片墙
     * @return void
     */
    public function deleteAssistantImage(){
        $id = $this->request->param('id');
        try {
            $result = AssistantManager::deleteAssistantImage($this->user,$id);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            } else {

                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 编辑助教信息
     * @return void
     */
    public function editAssistantInfo()
    {
        try {
            $result = AssistantManager::editAssistantInfo($this->user, $this->request->param());
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            } else {
                throw new Exception($result['message']);
            }
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }
    }

    /**
     * 获取助教订单列表
     * @return void
     */
    public function getOrderList()
    {
        $page = $this->request->param('page',1);
        $status = $this->request->param('status',-1);
        try {
            $result = AssistantManager::getOrderList($this->user,$status,$page);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'],$result['data']);
            } else {
                throw new Exception($result['message']);
            }
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }
    }
/*
    public function getOrderDetail()
    {
        try {
            $result = AssistantManager::getOrderDetail($this->user, $this->request->param('id'));
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'],$result['data']);
            } else {
                throw new Exception($result['message']);
            }
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }
    }*/

    /**
     * 助教确认订单
     * @return void
     */
    public function acceptOrder()
    {
        try {
            $result = AssistantManager::acceptOrder($this->user, $this->request->param('id'));
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            } else {
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 开始订单
     * @return void
     */
    public function setOrderStart()
    {
        try {
            $result = AssistantManager::setOrderStart($this->user,$this->request->param('id'));
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            } else {
                throw new Exception($result['message']);
            }
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }
    }

    /**
     * 结束订单
     * @return void
     */
    public function setOrderEnd()
    {
        try {
            $result = AssistantManager::setOrderEnd($this->user, $this->request->param('id'));
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            } else {
                throw new Exception($result['message']);
            }
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }
    }

    /**
     * 退单
     * @return void
     */
    public function refundOrder()
    {
        try {
            $result = AssistantManager::refundOrder($this->user, $this->request->param('id'));
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            } else {
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 设置接单状态
     * @return void
     */
    public function orderSwitch()
    {
        $status = $this->request->param('status');
        try {
            $result = AssistantManager::orderSwitch($this->user, $status);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 获取助教列表
     * @return void
     */
    public function getAssistantList(){
        $page = $this->request->param('page',1);
        $lng = $this->request->param('lng');
        $lat = $this->request->param('lat');
        $radius = $this->request->param('radius') ?? 100;
        $status = $this->request->param('status') ?? null;
        $recommend = $this->request->param('recommend') ?? null;
        try {
            $result = AssistantManager::getAssistantListCopy($this->user,$status,$recommend,$lng,$lat,$radius,$page);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'], $result['data']); // 助教列表
            } else {
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 设置时间
     * @return void
     */
    public function setTime()
    {
        $start_time = $this->request->param('start_time');
        $end_time = $this->request->param('end_time');
        try {
            $result = AssistantManager::setTime($this->user, $start_time, $end_time);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            } else {
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 编辑时间
     * @return void
     */
    public function editTime()
    {
        $start_time = $this->request->param('start_time');
        $end_time = $this->request->param('end_time');
        $id = $this->request->param('id');
        try {
            $result = AssistantManager::editTime($this->user, $start_time, $end_time,$id);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            } else {
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 删除时间
     * @return void
     */
    public function deleteTime()
    {
        $id = $this->request->param('id');
        try {
            $result = AssistantManager::deleteTime($this->user, $id);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            } else {
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 获取时间列表
     * @return void
     */
    public function timeList()
    {
        $page = $this->request->param('page',1);
        try {
            $result = AssistantManager::timeList($this->user,$page);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'], $result['data']);
            } else {
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 获取账户信息
     * @return void
     */
    public function getAccount(){
        try {
            $result = AssistantManager::getAccount($this->user->assistant_id,$this->user->id);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'], $result['data']);
            } else {
                throw new Exception($result['message']);
            }
        }catch (Exception $exception) {
            $this->error($exception->getMessage());
        }
    }

    /**
     * 获取余额日志
     * @return void
     */
    public function getMoneyLog()
    {
        $page = $this->request->param('page',1);
        $type = $this->request->param('type',0);
        $detail = $this->request->param('detail',0);
        try {
            $result = AssistantAccountManager::moneyLog($this->user->assistant_id,$this->user->id,$type,$detail,[],$page);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'], $result['data']);
            } else {
                throw new Exception($result['message']);
            }
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }
    }

    /**
     * 提现
     * @return void
     */
    public function withdrawMoney()
    {
        $money = $this->request->param('money');
        $type = $this->request->param('type','wechat');
        try {
            $result = AssistantAccountManager::withdrawMoney($this->user->assistant_id,$this->user->id,$money,$type);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            } else {
                throw new Exception($result['message']);
            }
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }
    }

    /**
     * 获取提现列表
     * @return void
     */
    public function getWithdrawList()
    {
        $page = $this->request->param('page',1);
        try {
            $result = AssistantAccountManager::getWithdrawList($this->user->assistant_id,$this->user->id,$page);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'], $result['data']);
            } else {
                throw new Exception($result['message']);
            }
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }
    }

    /**
     * 获取账列表
     * @return void
     */
    public function getBillList()
    {
        $page = $this->request->param('page',1);
        $type = $this->request->param('type',1);
        $time = $this->request->param('time',date('Y-m-d',time()));
        try {
            $result = AssistantAccountManager::getBillList($this->user->assistant_id,$this->user->id,$time,$type,$page);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'], $result['data']);
            } else {
                throw new Exception($result['message']);
            }
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }
    }

    public function checkAssistant()
    {
        $assistant_ids = $this->request->param('assistant_ids');
        $numbers = $this->request->param('numbers') ?? 0;
        $start_time = $this->request->param('start_time');
        $end_time = $this->request->param('end_time');
        try {
            $result = AssistantManager::checkAssistant($assistant_ids,$this->user->id,$numbers,strtotime($start_time),strtotime($end_time));
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            } else {
                throw new Exception($result['message']);
            }
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }


    }

    public function detail()
    {
        $assistant_id = $this->request->param('assistant_id');
        try {
            $result = AssistantManager::detail($assistant_id);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'], $result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }


    }

    public function invite()
    {
        $assistant_id = $this->request->param('assistant_id');
        try {
            $wechat = new Wechat();
            $page = config('wechat.assistant_invite_page');
            $scene = ['id'=>$assistant_id];
            $image = $result = $wechat->getUnlimited(http_build_query($scene),$page);
            $result = json_decode($result,true);
            if (json_last_error() == JSON_ERROR_NONE){
                if (isset($result['errcode']) && !empty($result['errcode'])){
                    throw new Exception($result['errmsg']);
                }
            }else{
                $this->success('获取成功',['image'=>base64_encode($image)]);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    public function getTags(){
        try {
            $result = AssistantManager::getTags();
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'], $result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    public function setTags()
    {
        try {
            $result = AssistantManager::setTags($this->user->assistant_id,$this->request->param('tags'));
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

}
